package cn.zzuli.springcloud.controller;

import cn.zzuli.commons.entity.CommonResult;
import cn.zzuli.commons.entity.Payment;
import cn.zzuli.springcloud.handler.CustomerBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Date: 2020/12/26 12:36
 * @Author: 绯色下午茶
 * @Description:
 */
@RestController
@RequestMapping("/sentinel")
public class RateLimitController {
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "deal_byResource")
    public CommonResult<Payment> byResource() {
        return new CommonResult<>(200, "按资源名称限流OK", new Payment(2020l, UUID.randomUUID().toString()));
    }

    public CommonResult deal_byResource(BlockException exception) {
        return new CommonResult(444, exception.getClass().getCanonicalName() + "\t资源不可用");
    }

    @GetMapping("/byURL")
    @SentinelResource(value = "byURL")
    public CommonResult<Payment> byURL() {
        return new CommonResult<>(200, "按url测试限流OK", new Payment(2020L, UUID.randomUUID().toString()));
    }


    @GetMapping("/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
//            指定处理全局限流的类
            blockHandlerClass = CustomerBlockHandler.class,
//            指定该限流处理类的某个方法
            blockHandler = "handleException2")
    public CommonResult<Payment> customerBlockHandler() {
        return new CommonResult<>(200, "按客户自定义测试限流OK",
                new Payment(2020L, UUID.randomUUID().toString()));
    }

}
